package com.gx.mxbbx.controller;

import com.github.pagehelper.PageInfo;
import com.gx.mxbbx.po.Area;
import com.gx.mxbbx.po.Disease;
import com.gx.mxbbx.service.DiseaseService;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;

@Controller
public class DiseaseController {
    @Autowired
    private DiseaseService diseaseService;

    @RequestMapping("/getdiseaseList")
    public String getdiseaseList(Integer currentPage, Integer limit, HttpServletRequest request, Model model) {
        String fullName = request.getParameter("context");
        currentPage = currentPage != null ? currentPage : 1;
        limit = limit != null ? limit : 10;

        PageInfo<Disease> pageInfo = diseaseService.getdiseaseList(fullName, currentPage, limit);

        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("context", fullName);
        return "/disease/disease_list";
    }

    @RequestMapping("/opendiseaseAdd")
    public String opendiseaseAdd() {
        return "/disease/disease_add";
    }

    @RequestMapping("/diseaseadd")
    public String diseaseadd(Disease disease, HttpServletRequest request, Model model) {
        diseaseService.diseaseadd(disease);
        return getdiseaseList(1, 10, request, model);
    }

    @RequestMapping("/diseaseopenEdit")
    public String diseaseopenEdit(Integer id, Model model) {
        Disease disease = diseaseService.diseaseopenEdit(id);
        model.addAttribute("disease", disease);
        return "/disease/disease_edit";
    }

    @RequestMapping("/diseaseedit")
    public String diseaseedit(Disease disease, HttpServletRequest request, Model model) {
        diseaseService.diseaseedit(disease);
        return getdiseaseList(1, 10, request, model);
    }

    @RequestMapping("/diseasedelete")
    public String diseasedelete(Integer id, HttpServletRequest request, Model model) {
        diseaseService.diseasedelete(id);
        return getdiseaseList(1, 10, request, model);
    }

    @RequestMapping("/diseaout")
    public void userExcel(HttpServletResponse response, Integer id) throws IOException {
        XSSFWorkbook wb = diseaseService.show(id);
        String fileName = "慢性病信息.xlsx";
        OutputStream outputStream = null;
        fileName = URLEncoder.encode(fileName, "UTF-8");
        //设置ContentType请求信息格式
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        outputStream = response.getOutputStream();
        wb.write(outputStream);
        outputStream.flush();
        outputStream.close();
    }
}
